package com.example.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.server.entity.Dingdan;
import com.example.server.entity.Order;
import com.example.server.service.DingdanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * (Dingdan)表控制层
 *
 * @author makejava
 * @since 2022-12-13 16:51:47
 */
@RestController
@RequestMapping("dingdan")
public class DingdanController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private DingdanService dingdanService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param dingdan 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Dingdan> page, Dingdan dingdan) {
        return success(this.dingdanService.page(page, new QueryWrapper<>(dingdan)));
    }

    /**
     * 通过主键查询单条数据
     * @return 单条数据
     */
    @GetMapping("{courseid}&&{userid}")
    public R selectOne(@PathVariable Serializable courseid,@PathVariable Serializable userid) {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("courseid",courseid);
        queryWrapper.eq("userid",userid);
        queryWrapper.eq("audit",1);
        Dingdan dindan = dingdanService.getBaseMapper().selectOne(queryWrapper);
        if(dindan == null)
        {
            r.setCode(400);
            r.setMsg("未购买");
            r.setData(dindan);

        }
        else {
            r.setCode(200);
            r.setMsg("已购买");
            r.setData(dindan);
        }
        return r;
    }

    /**
     * 新增数据
     *
     * @param dingdan 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Dingdan dingdan) {

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        dingdan.setCreatetime(date);
//        String replaceUUID = UUID.randomUUID().toString().replace("-", "");
//        dingdan.setDealnumber((long)dingdan.getCourseid());
        int rs = (int) ((Math.random() * 9 + 1) * Math.pow(10,9-1));
        dingdan.setDealnumber((long) rs);
        this.dingdanService.save(dingdan);
        R r =new R<>();
        r.setCode(200);
        r.setMsg("插入成功");
        r.setData(dingdan);
        return r;
    }

    /**
     * 修改数据
     *
     * @param dingdan 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Dingdan dingdan) {
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dealnumber",dingdan.getDealnumber());
        Dingdan din = new Dingdan();
        din.setAudit(1);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        din.setPaytime(date);
        dingdanService.update(din,queryWrapper);
        R r =new R<>();
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.dingdanService.removeByIds(idList));
    }
}

